﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFGame.Controls;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace WPFGame.Windows
{
    /// <summary>
    /// UserControl13.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl13 : UserControl
    {
        private Rectangle rect;//构造障碍物矩形
        private byte[,] Matrix = new byte[1024, 1024];
        private const int GridSize = 20;//单元格大小
        /// <summary>
        /// 记录上次寻径时标记在地图上的椭圆点,方便在下次寻径时清除
        /// </summary>
        private List<Ellipse> OldPathEllipse = new List<Ellipse>();

        public UserControl13()
        {
            InitializeComponent();
            InitMap();
            InitMask();
            InitPlayer();
            InitMatrix();
        }

        Hero Spirit = new Hero();
        /// <summary>
        /// 初始化角色
        /// </summary>
        private void InitPlayer()
        {
            Spirit.X = 500;
            Spirit.Y = 400;
            Spirit.HeroTimer.Interval = TimeSpan.FromMilliseconds(100);
            c.Children.Add(Spirit);
        }

        /// <summary>
        /// 初始化地图二维矩阵，以单元格值为0或1来判断是否有障碍物。
        /// </summary>
        private void InitMatrix()
        {
            for (int x = 0; x < Matrix.GetUpperBound(0); x++)
            {
                for (int y = 0; y < Matrix.GetUpperBound(1); y++)
                    Matrix[x, y] = 1;//默认为1表示可通过
            }

            //构建障碍物
            for (int y = 22; y <= 24; y++)
            {
                for (int x = 5; x <= 16; x++)
                {
                    //障碍物在矩阵中用0表示
                    Matrix[x, y] = 0;
                    rect = new Rectangle();
                    rect.Fill = new SolidColorBrush(Colors.GreenYellow);
                    rect.Opacity = 0.3;
                    rect.Stroke = new SolidColorBrush(Colors.Gray);
                    rect.Width = GridSize;
                    rect.Height = GridSize;
                    c.Children.Add(rect);
                    Canvas.SetLeft(rect, x * GridSize);
                    Canvas.SetTop(rect, y * GridSize);
                }
            }
            for (int y = 11; y <= 14; y++)
            {
                for (int x = 27; x <= 31; x++)
                {
                    //障碍物在矩阵中用0表示
                    Matrix[x, y] = 0;
                    rect = new Rectangle();
                    rect.Fill = new SolidColorBrush(Colors.GreenYellow);
                    rect.Opacity = 0.3;
                    rect.Stroke = new SolidColorBrush(Colors.Gray);
                    rect.Width = GridSize;
                    rect.Height = GridSize;
                    c.Children.Add(rect);
                    Canvas.SetLeft(rect, x * GridSize);
                    Canvas.SetTop(rect, y * GridSize);
                }
            }
            for (int y = 18; y <= 21; y++)
            {
                for (int x = 33; x <= 37; x++)
                {
                    //障碍物在矩阵中用0表示
                    Matrix[x, y] = 0;
                    rect = new Rectangle();
                    rect.Fill = new SolidColorBrush(Colors.GreenYellow);
                    rect.Opacity = 0.3;
                    rect.Stroke = new SolidColorBrush(Colors.Gray);
                    rect.Width = GridSize;
                    rect.Height = GridSize;
                    c.Children.Add(rect);
                    Canvas.SetLeft(rect, x * GridSize);
                    Canvas.SetTop(rect, y * GridSize);
                }
            }


        }

        Image Mask1, Mask2;
        /// <summary>
        /// 初始化地图遮罩层
        /// </summary>
        private void InitMask()
        {
            Mask1 = new Image() { Width = 238, Height = 244, Opacity = 0.7, Source = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Maps\Mask1.png", UriKind.RelativeOrAbsolute)) };
            Mask2 = new Image() { Width = 198, Height = 221, Opacity = 0.7, Source = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Maps\Mask2.png", UriKind.RelativeOrAbsolute)) };
            c.Children.Add(Mask1); c.Children.Add(Mask2);
            Canvas.SetLeft(Mask1, 185); Canvas.SetLeft(Mask2, 446);
            Canvas.SetTop(Mask1, 220); Canvas.SetTop(Mask2, 11);
            Canvas.SetZIndex(Mask1, 10000); Canvas.SetZIndex(Mask2, 10000);
        }



        Image Map;
        /// <summary>
        /// 初始化地图
        /// </summary>
        private void InitMap()
        {
            Map = new Image()
            {
                Width = 800,
                Height = 600,
                Source = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Maps\Map2.jpg", UriKind.RelativeOrAbsolute))
            };
            c.Children.Add(Map);
            Canvas.SetZIndex(Map, -1);
        }

        private void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
